// Copyright (c) Symbian Ltd 2008. All rights reserved.

// INCLUDE FILES

#include "BluetoothSocketWriterReader.h"
#include "common.hrh"
/*
============================================================================
CSocketReader's constructor
============================================================================
*/
CSocketReader::CSocketReader(RSocket& aBtSocket, MSocketWriterReaderObserver& aObserver) :
	CActive(CActive::EPriorityStandard), iBtSocket(aBtSocket), iObserver(aObserver)
    {
	CActiveScheduler::Add(this);
	}

/*
============================================================================
CSocketReader's destructor
============================================================================
*/
CSocketReader::~CSocketReader()
    {
    Cancel();
    }

/*
============================================================================
DoCancel is called as part of the active object's Cancel().
Cancels an outstanding BT socket read operation.
============================================================================
*/
void CSocketReader::DoCancel()
    {
	iBtSocket.CancelRead();
    }

/*
============================================================================
Handles the active object's reading data completion event.
On completion it calls the callback function MSocketWriterReaderObserver::ReportData
to pass to the caller the data received.
============================================================================
*/
void CSocketReader::RunL()
    {
	iObserver.ReportData(iBuffer, iStatus.Int());
    }	

/*
============================================================================
Asynchronous call to wait for incoming data
============================================================================
*/
void CSocketReader::ReadData()
    {
	if (!IsActive())
		{
		iBuffer.Zero();
		iBtSocket.RecvOneOrMore(iBuffer, 0, iStatus, iLen);
	    SetActive();
		}
    }

/*
============================================================================
Handles a leave occurring in the request completion event handler RunL().
============================================================================
*/
TInt CSocketReader::RunError(TInt aError)
	{
	iObserver.ReportData(iBuffer, aError);
	return KErrNone;
	}

///////////////////////////////////////////////////////////////////////////////////

/*
============================================================================
CSocketWriter's constructor
============================================================================
*/
CSocketWriter::CSocketWriter(RSocket& aBtSocket, MSocketWriterReaderObserver& aObserver) :
	CActive(CActive::EPriorityStandard), iBtSocket(aBtSocket), iObserver(aObserver) 
    {
	CActiveScheduler::Add(this);
	}

/*
============================================================================
CSocketWriter's destructor
============================================================================
*/
CSocketWriter::~CSocketWriter()
    {
    Cancel();
    }

/*
============================================================================
DoCancel is called as part of the active object's Cancel().
Cancels an outstanding BT socket write operation.
============================================================================
*/
void CSocketWriter::DoCancel()
    {
	iBtSocket.CancelWrite();
    }

/*
============================================================================
Handles the active object's writing data completion event.
On completion it calls the callback function MSocketWriterReaderObserver::WriteComplete
to notify the caller if the write was succesful
============================================================================
*/
void CSocketWriter::RunL()
	{
	iObserver.WriteComplete(iStatus.Int());
    }

/*
============================================================================
Asynchronous call to send data to the remote device.
Note that we handle only one event at a time. Data to write to the remote device
is not queued if an outstanding write operation is already in progress.
============================================================================
*/
TInt CSocketWriter::Write(const TDesC8& aBuf)
    {
	TInt err = KErrNone;

	if (!IsActive())
		{
		iBtSocket.Write(aBuf,iStatus);
		SetActive();
		}
	else
		{
		err = KErrInUse;
		}
	return(err);
    }

/*
============================================================================
Handles a leave occurring in the request completion event handler RunL().
============================================================================
*/
TInt CSocketWriter::RunError(TInt aError)
	{
	iObserver.WriteComplete(aError);
	return KErrNone;
	}
